package com.hengpeng.itfin.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.exception.ServiceResultFactory;
import com.hengpeng.itfin.service.PayService;
import com.hengpeng.itfinbase.constants.PaymentConstants;
import com.hengpeng.itfinbase.persist.AccountLog;
import com.hengpeng.itfinbase.persist.Payment;
import com.hengpeng.itfinbase.persist.Payment.PayStatus;
import com.hengpeng.itfinbase.persist.PaymentProvider;
import com.hengpeng.itfinbase.persist.PaymentProvider.PaymentProviderState;
import com.hengpeng.itfinbase.persist.PaymentProvider.PaymentProviderType;

/**
 * <pre>
 * 付款服务实现
 * 处理用户充值，提款
 * </pre>
 * @author shanguogang
 * @version 1.0, 2015-8-12
 */
@Transactional
@Service
@Lazy
public class PayServiceImpl extends BaseServiceImpl implements PayService
{
	public static final Logger logger = Logger.getLogger(PayServiceImpl.class);

	@Override
	public ServiceResult<Payment> dealPayResult(String providerId, String payOrderId, Long money, PayStatus payState,
			String message)
	{

		Payment payment = null;
		PaymentProvider provider = serviceFactory.getPayService().findProviderById(providerId);
		if ("-1".equals(providerId))
		{
			payment = serviceFactory.getPaymentService().findByOrderId(payOrderId);
		}
		else
		{
			payment = serviceFactory.getPaymentService().findByOrderIdAndProviderId(payOrderId, providerId);
		}
		if (payment != null)
		{
			payment = daoFactory.getCommonDao().lockObject(Payment.class, payment.getId());
			if (payment.getStatus() == PayStatus.PAYING)
			{
				if (payment.getMoney() != money)
				{// 充值金额不匹配
					return ServiceResultFactory.getServiceResult(
							PaymentConstants.PAYMENT_TRANSMESSAGE_ERRCODE_MISMONEY, payment);
				}
				// 成功更新金额
				if (payState == PayStatus.SUCCESS)
				{
					serviceFactory.getAccountService().add(payment.getUserId(), AccountLog.TransType.IMPREST,
							payment.getMoney(), String.valueOf(payment.getId()), provider.getMark());
				}
				else if (payState == PayStatus.PAYING)
				{
					return ServiceResultFactory.getServiceResult(PaymentConstants.PAYMENT_TRANSMESSAGE_ERRCODE_OTHER,
							payment, "充值结果响应状态错误:payState=" + payState);
				}
				payment.setStatus(payState);
				payment.setOverTime(new Date());
				payment.setUpdateTime(new Date());
				payment.setChargeMoney(0l);
				serviceFactory.getPersistService().update(payment);
				return ServiceResultFactory.getServiceResult(PaymentConstants.PAYMENT_TRANSMESSAGE_ERRCODE_SUCCESS,
						payment);
			}
			else if (payment.getStatus() == PayStatus.SUCCESS)
			{// 订单已完成,不能重复处理
				return ServiceResultFactory.getServiceResult(PaymentConstants.PAYMENT_TRANSMESSAGE_ERRCODE_SUCCESS,
						payment, "订单已完成,不能重复处理");
			}
			else
			{// 状态不正常
				return ServiceResultFactory.getServiceResult(PaymentConstants.PAYMENT_TRANSMESSAGE_ERRCODE_OTHER,
						payment, "交易状态不正常");
			}

		}
		else
		{// 未找到充值记录
			return ServiceResultFactory.getServiceResult(PaymentConstants.PAYMENT_TRANSMESSAGE_ERRCODE_MISTRANS,
					Payment.class);
		}
	}

	@Override
	public List<PaymentProvider> findProviderAutoPay()
	{
		return serviceFactory.getPersistService().findByNamedQuery(PaymentProvider.class,
				"paymentProvider.autoPayList", new Object[] { PaymentProviderType.DRAWING, PaymentProviderState.OPEN });
	}

	@Override
	public PaymentProvider findProviderById(String id)
	{
		return serviceFactory.getDaoFactory().getPaymentProviderDao().get(id);
	}

	@Override
	public List<PaymentProvider> listProviderByType(PaymentProviderType type)
	{
		return serviceFactory.getPersistService().findByNamedQuery(PaymentProvider.class, "paymentProvider.listByType",
				new Object[] { type });
	}
}
